package com.midea.thread;

import java.util.concurrent.*;

/**
 * @description：Submit和Execute的区别
 * execute和submit都属于线程池的方法，execute只能提交Runnable类型的任务，而submit既能提交Runnable类型任务也能提交Callable类型任务。
 * execute会直接抛出任务执行时的异常，submit会吃掉异常，可通过Future的get方法将任务执行时的异常重新抛出。
 * execute所属顶层接口是Executor,submit所属顶层接口是ExecutorService，实现类ThreadPoolExecutor重写了execute方法,抽象类AbstractExecutorService重写了submit方法。
 * @author: wangwl23
 * @create: 2022-03-24 14:57
 */
public class TestSubmitAndExecute {
    static ExecutorService executor = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        initExecutors();


//        Object object = testSubmitRunnable(new RunnableTask());
//        System.out.println(object);
//        testExecuteRunnable(new RunnableTask());



        /**
         * <T> Future<T> submit(Runnable task, T result);
         * 可以看出我们输入的什么参数，任务执行完毕后就返回什么参数。
         */
//        Integer i = testSubmitRunnable(new RunnableTask(), 3);
//        System.out.println(i);
//        Boolean bool = testSubmitRunnable(new RunnableTask(), true);
//        System.out.println(bool);
//        String str = testSubmitRunnable(new RunnableTask(), "你好吗");
//        System.out.println(str);

        /**
         * execute会直接抛出任务执行时的异常，submit会吃掉异常，可通过Future的get方法将任务执行时的异常重新抛出。
         */
//        Object o = testSubmitCallable(new CallableTask());
//        System.out.println(o);

        /**
         * execute会直接抛出任务执行时的异常，submit会吃掉异常，可通过Future的get方法将任务执行时的异常重新抛出。
         */
        testExecuteRunnable(new ExceptionRunableTask());
        System.out.println("------------");
        testSubmitCallable(new ExceptionCallableTask());

        waitToTerminated();

    }


    private static void initExecutors() {
        if (executor.isTerminated()) {
            executor = Executors.newCachedThreadPool();
        }
    }

    private static void waitToTerminated() {
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
    }

    /**
     * 测试 submit(Callable<T> task)
     *
     * @param callable
     * @param <T>
     * @return
     */
    public static <T> T testSubmitCallable(Callable callable) {
        Future<T> future = executor.submit(callable);
        T result = null;
        /*try {
            result = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }*/
        return result;
    }

    /**
     * 测试submit(Runnable task, T result)
     *
     * @param runnable
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T testSubmitRunnable(Runnable runnable, T t) {
        Future<T> future = executor.submit(runnable, t);
        T result = null;
        try {
            result = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 测试 submit(Runnable task)
     * submit提交Runnable任务会默认返回null
     *
     * @param runnable
     * @return
     */
    public static Object testSubmitRunnable(Runnable runnable) {
        Future<?> future = executor.submit(runnable);
        Object v = null;
        try {
            v = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return v;
    }

    /**
     * 测试 execute(Runnable command)
     * execute会直接抛出异常，submit只有通过调用Future对象的get方法才能获取异常
     *
     * @param runnable
     */
    public static void testExecuteRunnable(Runnable runnable) {
        executor.execute(runnable);
    }
}


class CallableTask implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 520; i++) {
            sum += i;
        }
        return sum;
    }
}

/**
 * 会抛异常的CallableTask
 */
class ExceptionCallableTask implements Callable<Boolean> {
    public Boolean call() throws Exception {
        int num = 1 / 0;
        return false;
    }
}

class RunnableTask implements Runnable {
    @Override
    public void run() {
        System.out.println("I am a runnable task");
    }
}

/**
 * 会抛异常的RunnableTask
 */
class ExceptionRunableTask implements Runnable {
    @Override
    public void run() {
        int num = 1 / 0;
    }
}
